﻿from docx import Document
import os
import string

from docx.shared import Pt
from openpyxl import load_workbook
import random
from datetime import datetime, timedelta
from docx.enum.text import WD_ALIGN_PARAGRAPH


def gen_us_date():
    start_date = datetime(2019, 4, 30)  # 定义起始日期
    end_date = datetime(2023, 3, 20)  # 定义结束日期
    days = (end_date - start_date).days  # 计算日期范围内的天数
    random_days = random.randint(0, days)  # 随机生成一个天数
    random_date = start_date + timedelta(days=random_days)  # 在起始日期的基础上增加随机天数得到随机日期
    us_date = datetime.strftime(random_date, '%m/%d/%Y')
    # print(us_date)
    us_date_tb = us_date.split('/')
    year = us_date_tb[2]
    year = year[2:4]
    us_date = us_date_tb[0] + '/' + us_date_tb[1] + '/' + year
    # print(us_date)
    return us_date


def random_time_between(start_time, end_time):
    """
    生成指定时间范围内的随机时间
    :param start_time: 起始时间字符串，如 '08:00:00'
    :param end_time: 结束时间字符串，如 '18:00:00'
    :return: 随机时间的datetime对象
    """
    start_dt = datetime.strptime(start_time, '%H:%M:%S')
    end_dt = datetime.strptime(end_time, '%H:%M:%S')
    random_seconds = random.randint(0, int((end_dt - start_dt).total_seconds()))
    random_dt = start_dt + timedelta(seconds=random_seconds)
    day_time = datetime.strftime(random_dt, '%H:%M:%S')
    return day_time


# 生成包含大写字母和数字的随机字符串
def generate_random_string(length):
    chars = string.digits + string.ascii_uppercase
    return ''.join(random.choice(chars) for _ in range(length))


def generate_random_number(length):
    # 生成一个0到9之间的随机整数
    random_digit = random.randint(0, 9)
    # 重复生成3次，每次生成的数是随机0-9的整数
    random_digits = [random.randint(0, 9) for _ in range(length - 1)]
    # 将第一个随机数和其余的三个随机数连接成一个列表
    all_digits = [random_digit] + random_digits
    # 使用join将列表元素转换成一个字符串
    four_digit_number = ''.join(map(str, all_digits))
    # print(four_digit_number)
    return four_digit_number


def create_directory(path):
    if not os.path.exists(path):
        os.mkdir(path)


print("任务开始")
save_path = os.path.abspath("存放目录")
create_directory(save_path)

filename = os.path.abspath("demo.docx")

# 加载工作簿
xlsx_name = os.path.abspath("沃尔玛地址.xlsx")
workbook = load_workbook(filename=xlsx_name)

# 选择工作表
sheet = workbook.active
print("地址读取完成")
ziti_geshi = "A-R-ZHENDAIGOU"
ziti_daxiao = 8

# 遍历工作表中的所有行
for row in sheet.iter_rows(values_only=True):
    phone = row[0]
    if phone != "店铺电话号码":
        # 加载现有的Word文档
        doc = Document(filename)
        # 修改文档中的内容

        for para in doc.paragraphs:
            # print(para.text)
            if 'aaaaaaaaaaa' in para.text:
                for run in para.runs:
                    # 如果run的文本中包含目标字符串，则替换该run中的文本
                    if 'aaaaaaaaaaa' in run.text:
                        run.text = run.text.replace('aaaaaaaaaaa', generate_random_string(11))
                        run.font.name = ziti_geshi
                        run.font.size = Pt(ziti_daxiao)

            if '{shop}' in para.text:
                for run in para.runs:
                    if '{shop}' in run.text:
                        run.text = run.text.replace('{shop}', row[6])
                        run.font.name = ziti_geshi
                        run.font.size = Pt(ziti_daxiao)

                    if '{4}' in run.text:
                        run.text = run.text.replace('{4}', generate_random_number(4))
                        run.font.name = ziti_geshi
                        run.font.size = Pt(ziti_daxiao)

                    if '{5}' in run.text:
                        run.text = run.text.replace('{5}', str(random.randint(20, 200)))
                        run.font.name = ziti_geshi
                        run.font.size = Pt(ziti_daxiao)

                    if '{6}' in run.text:
                        run.text = run.text.replace('{6}', generate_random_number(4))
                        run.font.name = ziti_geshi
                        run.font.size = Pt(ziti_daxiao)

            if '0000 0000 0000 0000 0000' in para.text:
                wuduan_num = generate_random_number(4) + " " + generate_random_number(4) + " " + generate_random_number(
                    4) + " " + generate_random_number(4) + " " + generate_random_number(4)
                para.text = para.text.replace('0000 0000 0000 0000 0000', wuduan_num)
                para.runs[0].font.name = ziti_geshi
                para.runs[0].font.size = Pt(8)

            if '{phone}' in para.text:
                para.text = para.text.replace('{phone}', phone)
                para.runs[0].font.name = ziti_geshi
                para.runs[0].font.size = Pt(ziti_daxiao)

            if '{name}' in para.text:
                para.text = para.text.replace('{name}', row[1])
                para.runs[0].font.name = ziti_geshi
                para.runs[0].font.size = Pt(ziti_daxiao)
                para.alignment = WD_ALIGN_PARAGRAPH.CENTER

            if '{addRess}' in para.text:
                para.text = para.text.replace('{addRess}', row[2])
                para.runs[0].font.name = ziti_geshi
                para.runs[0].font.size = Pt(ziti_daxiao)
                para.alignment = WD_ALIGN_PARAGRAPH.CENTER

            if '{cs} {zhou} {zip}' in para.text:
                para.text = para.text.replace('{cs} {zhou} {zip}', row[3] + "  " + row[4] + "  " + str(row[5]))
                para.runs[0].font.name = ziti_geshi
                para.runs[0].font.size = Pt(ziti_daxiao)
                para.alignment = WD_ALIGN_PARAGRAPH.CENTER

            if '00/00/00' in para.text:
                para.text = para.text.replace('00/00/00', gen_us_date())
                para.runs[0].font.name = ziti_geshi
                para.runs[0].font.size = Pt(8)

            if '00:00:00' in para.text:
                para.text = para.text.replace('00:00:00', random_time_between('08:00:00', '18:00:00'))
                para.runs[0].font.name = ziti_geshi
                para.runs[0].font.size = Pt(8)

        # 保存修改后的文档
        save_name = os.path.join(save_path, phone + ".docx")
        # save_name = os.path.abspath(phone+".doc")
        doc.save(save_name)
        print("成功生成：" + phone + ".docx")